<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/7/27
 * Time: 17:16
 */

namespace Admin\Service;


class ScoreCommentService
{
    /**
     * 获取谱单评论
     */
    public function getCommentByScoreId($scoreId)
    {
        $model = M('ScoreComment');
        if (empty($scoreId)) {
            $map = array('is_delete' => 0);
            $list = $model->field('id, score_id, user_id, remark, create_time')
                ->where($map)->select();
            //添加谱单名
            $scoreIds = array_column($list, 'score_id');
            $score = new ScoreService();
            $scoreMap = $score->getScoreInfoByIds($scoreIds);

            //添加用户昵称
            $userIds = array_column($list,'user_id');
            $user = new UserService();
            $userMap = $user->getUserInfoByIds($userIds);
            foreach ($list as $key => $val) {
                $list[$key]['score_name'] = $scoreMap[$val['score_id']]['name'];
                $list[$key]['user_name'] = $userMap[$val['user_id']]['nick_name'];
            }
            return $list;

        } else {
            $map = array(
                'score_id' => $scoreId,
                'is_delete' => 0
            );
            $list = $model->field('id, score_id, user_id, remark, create_time')
                ->where($map)->select();
            //添加谱单名
            $scoreName = M('Score')->where(array('id'=>$scoreId))->getField('name');
            //添加用户昵称
            $userIds = array_column($list,'user_id');
            $user = new UserService();
            $userMap = $user->getUserInfoByIds($userIds);
            foreach ($list as $key => $val) {
                $list[$key]['score_name'] = $scoreName;
                $list[$key]['user_name'] = $userMap[$val['user_id']]['nickname'];
            }
            return $list;
        }

    }

    /**
     * 获取谱单评论
     */
    public function getCommentByParam($param)
    {
        $where = array();
        if(!empty($param['score_id'])) {
            $scoreIds = $param['score_id'];
        }
        /*if(!empty($param['name'])) {
            $where['name'] = array('like', trim($param['name']).'%');
            $scoreArr = M('score')->field('id')->where($where)->select();
            if(empty($scoreArr)) return array();
            $scoreIds = array_column(array_unique($scoreArr), "id");
        }*/

        $model = M('ScoreComment');
        $map['is_delete'] = array('eq', 0);
        if(!empty($scoreIds)) $map["score_id"] = array('in', $scoreIds);
        $list = $model->field('id, score_id, user_id, remark, create_time')->where($map)->select();
        //添加谱单名
        $scoreIds = array_column(array_unique($list), 'score_id');
        $score = new ScoreService();
        $scoreMap = $score->getScoreInfoByIds($scoreIds);

        //添加用户昵称
        $userIds = array_column($list,'user_id');
        $user = new UserService();
        $userMap = $user->getUserInfoByIds($userIds);
        foreach ($list as $key => $val) {
            $list[$key]['score_name'] = $scoreMap[$val['score_id']]['name'];
            $list[$key]['user_name'] = $userMap[$val['user_id']]['nickname'];
        }
        return $list;
    
    }

    /**
     * 获取谱单评论数
     */
    public function multiCommentCount($scoreIds)
    {
        $model = M('ScoreComment');
        $map = array(
            'score_id' => array('in', $scoreIds),
            'is_delete' => 0
        );

        $res = $model->field('score_id, count(*)')->where($map)->group('score_id')->select();
        $commentCountMap = array();
        foreach ($res as $key => $val) {
            $commentCountMap[$val['score_id']] = $val['count(*)'];
        }
        return $commentCountMap;
    }

    /**
     * 添加谱单评论
     * @param $param
     * @return mixed
     */
    public function add($param)
    {
        $model = M('ScoreComment');
        $data = $model->create($param);
        if ($data) {
            //补充参数
            $param['user_id'] = $param['uid'];
            $id = $model->add($param);

            //谱单评论数 +1
            M('Score')->where(array('id'=>$param['score_id']))->setInc('comment_count');

            return $id;
        } else {
            E($model->getError());
        }
    }
}